package com.hailian.fintech.hlfinanceserver.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hailian.fintech.hlcomm_interfaceserver.dto.YingyeDto;
import com.hailian.fintech.hlcomm_interfaceserver.entity.POJO;
import com.hailian.fintech.hlcomm_interfaceserver.util.ExcelBean;
import com.hailian.fintech.hlcomm_interfaceserver.util.ExcelUtils;
import com.hailian.fintech.hlfinanceserver.entity.Yingye;
import com.hailian.fintech.hlfinanceserver.mapper.YingyeMapper;
import com.hailian.fintech.hlfinanceserver.service.IYingyeService;
import com.hailian.fintech.hlfinanceserver.vo.YingyeVo;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.IntrospectionException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wk
 * @since 2020-05-23
 */
@Service
public class YingyeServiceImpl extends ServiceImpl<YingyeMapper, Yingye> implements IYingyeService {

    @Autowired
    private YingyeMapper yingyeMapper;

    @Override
    public XSSFWorkbook downLoadGoodsExcel(YingyeDto yingyeDto) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {

        PageHelper.startPage(yingyeDto.getPageNo(),yingyeDto.getPageSize());
        List<Yingye> list = yingyeMapper.selectAllOwner(new POJO());

        PageInfo pageInfo = new PageInfo(list);
        Map<Integer, List<ExcelBean>> map = new HashMap<Integer, List<ExcelBean>>();

        List<ExcelBean> excelBeans = new ArrayList<>();
        excelBeans.add(new ExcelBean("id","id",0));
        excelBeans.add(new ExcelBean("交易时间","deal_time",0));
        excelBeans.add(new ExcelBean("总计","sum",0));
        excelBeans.add(new ExcelBean("押金","pledge",0));
        excelBeans.add(new ExcelBean("租金","rent",0));
        excelBeans.add(new ExcelBean("电费","electric",0));
        excelBeans.add(new ExcelBean("宽带","water",0));
        excelBeans.add(new ExcelBean("有线电视","gas_fee",0));
        excelBeans.add(new ExcelBean("其他","orther",0));
        map.put(0,excelBeans);
        XSSFWorkbook book = ExcelUtils.createExcelFile(Yingye.class, pageInfo.getList(), map, "营业报表");
        return book;
    }

    @Override
    public void uploadExcel(InputStream inputStream, String originalFilename) throws Exception {
        List<List<Object>> bankListByExcel = ExcelUtils.getBankListByExcel(inputStream, originalFilename);
        for (List<Object> objects : bankListByExcel) {
            Yingye yingye = new Yingye();

            //yingye.setNumber(String.valueOf(objects.get(1)));
            //yingye.setConsumerId(Integer.valueOf(objects.get(2).toString()));
          /*  baoli.setInventoryQuantity(Integer.valueOf(objects.get(3).toString()));
            baoli.setPurchasingPrice(Float.valueOf(objects.get(4).toString()));
            baoli.setSellingPrice(Float.valueOf(objects.get(5).toString()));
            baoli.setUnit(objects.get(6).toString());
            baoli.setLastPurchasingPrice(Float.valueOf(objects.get(7).toString()));
            baoli.setState(2);
            baoli.setModel(objects.get(8).toString());
            baoli.setMinNum(100);*/
            //yingyeMapper.insert(baoli);
        }
    }

    @Override
    public IPage<YingyeVo> selectPageVo(Page<?> page, YingyeVo yingyeVo) {
        return yingyeMapper.selectPageVo(page, yingyeVo);
    }

}
